package cn.hzy.toolkit;

/**
 * <p>
 *  ApiResult Ajax 返回 对象
 * </p>
 *
 * @author hzy
 * @since 2020-04-26
 */
public class ApiResult {

    public ApiResult(int status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public ApiResult(StatusCodeEnum statusCodeEnum, String msg, Object data) {
        this.status = statusCodeEnum.getValue();
        this.msg = msg;
        this.data = data;
    }

    private int status;
    private String msg;
    private Object data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * ApiResult status 的枚举值
     */
    public enum StatusCodeEnum {
        /**
         * 程序异常
         */
        Error(-2),
        /**
         * 未授权
         */
        UnAuth(-1),
        /**
         * 警告
         */
        Warn(0),
        /**
         * 成功
         */
        Ok(1);

        StatusCodeEnum(int value) {
            this.value = value;
        }

        private final int value;

        public int getValue() {
            return this.value;
        }
    }

    /**
     * 返回
     *
     * @param status
     * @param msg
     * @return ApiResult
     */
    public static ApiResult result(int status, String msg) {
        return new ApiResult(status, msg, null);
    }

    /**
     * 返回
     *
     * @param status
     * @param data
     * @return ApiResult
     */
    public static ApiResult result(int status, Object data) {
        return new ApiResult(status, "result", data);
    }

    /**
     * 返回
     *
     * @param status
     * @param msg
     * @param data
     * @return ApiResult
     */
    public static ApiResult result(int status, String msg, Object data) {
        return new ApiResult(status, msg, data);
    }

    /**
     * 返回
     *
     * @param status
     * @param msg
     * @return ApiResult
     */
    public static ApiResult result(StatusCodeEnum status, String msg) {
        return new ApiResult(status, msg, null);
    }

    /**
     * 返回
     *
     * @param status
     * @param data
     * @return ApiResult
     */
    public static ApiResult result(StatusCodeEnum status, Object data) {
        return new ApiResult(status, "result", data);
    }

    /**
     * 返回
     *
     * @param status
     * @param msg
     * @param data
     * @return ApiResult
     */
    public static ApiResult result(StatusCodeEnum status, String msg, Object data) {
        return new ApiResult(status, msg, data);
    }

    /**
     * 成功
     *
     * @return ApiResult
     */
    public static ApiResult resultOk() {
        return ApiResult.result(StatusCodeEnum.Ok, "success");
    }

    /**
     * 成功
     *
     * @param msg 消息
     * @return ApiResult
     */
    public static ApiResult resultOk(String msg) {
        return ApiResult.result(StatusCodeEnum.Ok, msg);
    }

    /**
     * 成功
     *
     * @param data 数据
     * @return ApiResult
     */
    public static ApiResult resultOk(Object data) {
        return ApiResult.result(StatusCodeEnum.Ok, "success", data);
    }

    /**
     * 成功
     *
     * @param msg
     * @param data
     * @return ApiResult
     */
    public static ApiResult resultOk(String msg, Object data) {
        return ApiResult.result(StatusCodeEnum.Ok, msg, data);
    }

    /**
     * 警告
     *
     * @return ApiResult
     */
    public static ApiResult resultWarn() {
        return ApiResult.result(StatusCodeEnum.Warn, "warn");
    }

    /**
     * 警告
     *
     * @param msg
     * @return ApiResult
     */
    public static ApiResult resultWarn(String msg) {
        return ApiResult.result(StatusCodeEnum.Warn, msg);
    }

    /**
     * 警告
     *
     * @param data
     * @return ApiResult
     */
    public static ApiResult resultWarn(Object data) {
        return ApiResult.result(StatusCodeEnum.Warn, "warn", data);
    }

    /**
     * 警告
     *
     * @param msg
     * @param data
     * @return ApiResult
     */
    public static ApiResult resultWarn(String msg, Object data) {
        return ApiResult.result(StatusCodeEnum.Warn, msg, data);
    }

}
